<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<title>月光边境</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%@ include file="/WEB-INF/jsp/common/style_easyui.jsp"%>
<style>
a.active{background: #0ae;color: #fff}
a[class*="btn-warning"].active{background: #f60;color: #fff}
select{height: 28px;border: 1px solid #0ae;}
a[data-name]{margin-right: 5px}
</style>
<body>
	<div id="bar" class="easyui-toolbar datagrid-toolbar" >
		<a class="easyui-linkbutton btn-success" data-options="iconCls:'fa fa-plus'" id="addBt">新增</a>
		<span id="sysBtl">
		<%-- <c:forEach items="${templates }" var="template">
		<c:if test="${template=='bean'||template=='controller'||template=='jsp_form'||template=='jsp_list'||template=='mapperJava'||template=='mapperXml'||template=='service'||template=='serviceImpl' }">
		<a class="easyui-linkbutton btn-primary" data-name="${template }">${template }</a>
		</c:if>
		</c:forEach> --%>
		</span>
		
		<span id="userBtl">
		<%-- <c:forEach items="${templates }" var="template">
		<c:if test="${template!='bean'&&template!='controller'&&template!='jsp_form'&&template!='jsp_list'&&template!='mapperJava'&&template!='mapperXml'&&template!='service'&&template!='serviceImpl' }">
		<a class="easyui-linkbutton btn-warning" data-name="${template }">${template }</a>
		</c:if>
		</c:forEach> --%>
		</span>
		<select id="-mode">
		<option value="csharp">C#</option>
		<option value="css">CSS</option>
		<option value="ftl">FreeMarker</option>
		<option value="html">HTML</option>
		<option value="java">Java</option>
		<option value="javascript">JavaScript</option>
		<option value="json">JSON</option>
		<option value="jsp">JSP</option>
		<option value="mysql">MySQL</option>
		<option value="python">Python</option>
		<option value="sql">SQL</option>
		<option value="sqlserver">SQLServer</option>
		<option value="text">Text</option>
		<option value="xml">XML</option>
		<option value="yaml">YAML</option>
		</select>
	</div>
	<div style="height: 100%">
		<textarea class="aceEditor" data-mode="java" style="position: absolute;left: 0;right: 0;top: 40px;bottom: 40px;margin: 0"></textarea>
	</div>
	<div class="easyui-toolbar datagrid-toolbar" style="position: fixed;bottom: 0;width: 100%;border-top:1px solid #ddd">
		<a class="easyui-linkbutton btn-success" data-options="iconCls:'fa fa-save'" onclick="saveTemplate()">保存</a>
		<a class="easyui-linkbutton btn-danger" data-options="iconCls:'fa fa-remove'" style="display: none" id="delBt">删除</a>
	</div>
	<!-- 表单弹出层 -->
	<div style="display: none">
	    <div id="dialog" data-options="buttons:'#btns'">
		    <form id="form" method="post">
			    <table class="table-form">
			        <tr>
			            <td align="right">模板名称：</td>
			            <td>
			                <input name="name" class="easyui-textbox" data-options="width:200,required:true,validType:{reg:[/^[A-Za-z]+[A-Za-z0-9_]*$/,'英文字母开头，加数字、下划线'],rule_ext_name:[]}" />
			            </td>
			        </tr>
		        </table>
	        </form>
	    </div>
	    <div id="btns">
	        <a class="easyui-linkbutton btn-success" data-options="iconCls:'fa fa-save'" onclick="addTemplate()">保存</a>
	        <a class="easyui-linkbutton btn-danger" data-options="iconCls:'fa fa-close'" onclick="$('#dialog').dialog('close')">关闭</a>
	    </div>
	</div>
</body>
<script>
$(function(){
	$(document).on('click','.easyui-linkbutton[data-name]', function(){
		$('.easyui-linkbutton[data-name]').removeClass('active');
		$(this).addClass('active');
		$.ajax({
			url:'${ctx}/coder/templates/'+$(this).data('name'),
			dataType:'json',
			success:function(data){
				$('textarea').data('editor').setValue(data.content);
				$('textarea').data('editor').moveCursorTo(0, 0);
			}
		});
		if($(this).data('name').toString().indexOf('Xml')>-1 || $(this).data('name').toString().indexOf('jsp')>-1){
			$('select').val('jsp').trigger('change');
		}else{
			$('select').val('java').trigger('change');
		}
		if($(this).hasClass('btn-warning')){
			$('select').val('jsp').trigger('change');
			$('#delBt').show();
		}else{
			$('#delBt').hide();
		}
	});
	$('select').val('java');
	$('select').change(function(){
		$('textarea').data('mode',$(this).val());
		$('textarea').data('editor').refreshMode();
	});
	
	$('#addBt').click(function(){
		$('#form').form('clear');
		$('#dialog').dialog({
			title : '新增',
			width : 400,
			height : 200,
			cache : false,
			modal : true,
		});
	});
	$('#delBt').click(function(){
		$.messager.confirm('提示', '确定删除数据吗', function(r) {
			if (r) {
				fileName = $('.active[data-name]').data('name')
				$.get('${ctx}/coder/templates/'+fileName+'/del',function(result){
					refresh();
				});
			}
		});
	});
	refresh();
});
//校验名称重复
$.extend($.fn.validatebox.defaults.rules, {
    rule_ext_name: {
		validator: function(value){
			var flag = true;
			$('.easyui-linkbutton[data-name]').each(function(){
				if($(this).data('name') == value){
					flag = false;
					return flag;
				}
			});
			return flag;
		},
		message: '已存在'
    }
});
function addTemplate(){
	if(!$('#form').form('validate')){
		return;
	}
	var fileName = $('#form input[name="name"]').val();
	saveTemplate(fileName);
	$('#dialog').dialog('close');
}
function saveTemplate(name){
	fileName = name?name:$('.active[data-name]').data('name')
	$.ajax({
		url:'${ctx}/coder/templates/'+fileName+'/edit',
		type:'POST',
		dataType:'json',
		async: false,
		data:{
			content:$('textarea').val()
		},
		success:function(result){
			if(result.retCode == 200){
				$.messager.show({
					title : '提示',
					msg : '操作成功',
					timeout : 2000,
					showType : 'slide'
				});
				refresh(fileName);
			}
		}
	});
}
function refresh(name){
	$.getJSON('${ctx}/coder/templates.json',function(result){
		$('#sysBtl').empty();
		$('#userBtl').empty();
		$.each(result, function(i, template){
			if(template=='bean'||template=='controller'||template=='jsp_form'||template=='jsp_list'||template=='mapperJava'||template=='mapperXml'||template=='service'||template=='serviceImpl'){
				html = '<a class="easyui-linkbutton btn-primary" data-name="'+template+'">'+template+'</a>';
				$('#sysBtl').append(html);
			}else{
				html = '<a class="easyui-linkbutton btn-warning" data-name="'+template+'">'+template+'</a>';
				$('#userBtl').append(html);
			}
		});
		if(name){
			$('.easyui-linkbutton[data-name="'+name+'"]').click();
		}else{
			$('.easyui-linkbutton[data-name]').first().click();
		}
		$.parser.parse();
	});
}
</script>
<script type="text/javascript" src="${ctx }/tools/js/format.js"></script>
<script type="text/javascript" src="${ctx }/plugins/ace-editor/ace.js"></script>
</html>